/** https://github.com/shadowOfCode/bee.js */
const Validation = {
    //手机号码
    isMobile: function (input) {
        return /^(?:\+86)?(?:13\d|14[57]|15[0-35-9]|17[35-8]|18\d)\d{8}$|^(?:\+86)?170[057-9]\d{7}$/.test(input);
    },
    //手机号码简单校验，只校验长度
    isMobileSize: function (input) {
        return /^(?:\+86)?1\d{10}$/.test(input);
    },
    isPhone: function (input) {
        return /^(?:\(\d{3,4}\)|\d{3,4}-)?\d{7,8}(?:-\d{1,4})?$/.test(input);
    },
    isCall: (input) => {
        return this.isPhone(input) || this.isMobile(input);
    },
    //邮箱格式校验
    isEmail: function (input) {
        return /^[-\w+]+(?:\.[-\w]+)*@[-a-z0-9]+(?:\.[a-z0-9]+)*(?:\.[a-z]{2,})$/i.test(input);
    },
    //18位身份证简单校验
    isIdCardSimple: function (idCard) {
        return /^(?:1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5])\d{4}(?:1[89]|20)\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{3}(?:\d|[xX])$/.test(idCard);
    },
    // 真实姓名校验
    isName: function (input) {
        return /^([\u4e00-\u9fa5]{1,20}|[a-zA-Z.\s]{1,20})$/.test(input);
    },
    //
    //18位身份证严格校验
    isIdCard: function (idCard) {
        let checkCode = (input) => {
            var multiplier = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            var ids = input.split("");
            var len = 17, sum = 0;
            for (var i = 0; i < len; i++) {
                sum += ids[i] * multiplier[i];
            }
            var remainder = sum % 11;
            var checkCodeArr = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
            var result = checkCodeArr[remainder];
            return result === input[17];
        }
        //先校验格式
        if (this.isIdCardSimple(idCard)) {
            //校验日期时间是否合法
            var dateStr = idCard.substr(6, 8);
            var dateStrNew = dateStr.replace(/(\d{4})(\d{2})(\d{2})/, "$1/$2/$3");
            var dateObj = new Date(dateStrNew);
            var month = dateObj.getMonth() + 1;
            if (parseInt(dateStr.substr(4, 2)) === month) {
                return checkCode(idCard);
            }
        }
        return false;
    },
    getFromIdCard: (idCard) => {
        var area = idCard.substr(0, 6);
        var birth = idCard.substr(6, 8).replace(/(\d{4})(\d{2})(\d{2})/, '$1年$2月$3日');
        var age = new Date().getFullYear() - idCard.substr(6, 4) + 1;
        var sex = (idCard.substr(16, 1) % 2 === 0) ? '女' : '男';

        return {
            'area': area,
            'birth': birth,
            'age': age,
            'sex': sex
        };
    }
};

export default Validation;